var CLBackgroundLayer = cc.Layer.extend({
    scrollView          : null, //滚动图层
    zOrderMap           : {},   // 层级枚举
    routeButtonArray    :[],    //关卡数组
    ctor:function () {
       this._super();
       cc.log("====CLBackgroundLayer ctor=======");

    },

    onEnter:function () {
        cc.log("====CLBackgroundLayer onEnter=======");
        this._super();
        //加载 属性】
        this.loadProperty();
        //加载【滚动图层】
        this.loadScrollView();
        //加载【瓦片地图】
        this.loadTiledMap();
        //加载【关卡】
        this.loadlevel(2);
    },

    // 加载层级属性
    loadProperty:function () {
        this.zOrderMap.route = 1;               // 道路
        this.zOrderMap.routeButtonEffect = 5;   // 按钮特效
        this.zOrderMap.leaveButton = 10;        // 按钮
        this.routeButtonArray = [];
    },


    //加载滚动图层
    loadScrollView:function () {
        var node = new ccui.ScrollView();
        this.addChild(node);
        this.scrollView = node;
        node.setContentSize(cc.winSize);
        node.setTouchEnabled(true);
        node.setDirection(ccui.ScrollView.DIR_HORIZONTAL);

        var nextPosX = 0;
        var imageView = null;
        for(var i =0;i<14;i++) {
            var imageName = "cl_stage_map_"+i+"_png";
            imageView = new ccui.ImageView(res[imageName]);
            node.addChild(imageView);
            imageView.setAnchorPoint(cc.p(0,0.5));
            imageView.setPosition(cc.p(nextPosX,cc.winSize.height/2));
            nextPosX += imageView.width;
        }
        node.setInnerContainerSize(cc.size(nextPosX,cc.winSize.height));
    },

    //加载瓦片地图
    loadTiledMap:function () {//?
        var node = new cc.TMXTiledMap(res.cl_road_tiledMap_tmx);
        var obgectGroup = node.getObjectGroup("point");
        var objs = obgectGroup.getObjects();
        for(var i =0; i< objs.length;i++){
            var button = new ccui.Button();
            this.scrollView.addChild(button,this.zOrderMap.leaveButton,i);
            this.routeButtonArray.push(button);
            //图片纹理
             var texture = res.cl_stagepoint_adv_png;
             if(objs[i].isBoos === "YES"){
                 texture = res.cl_stagepoint_gate_png;
             }else if(objs[i].isTime === "YES"){
                 texture = res.cl_stagepoint_time_png;
             }else if(objs[i].isChange === "YES"){
                 texture = res.cl_stagepoint_chance_png;
             }else{
                 texture = res.cl_stagepoint_adv_png;
             }

             button.loadTextures(texture,texture,"");
             button.setPosition(objs[i].x,objs[i].y);
             button.setTag(i);
             button.addTouchEventListener(this.onLevelButtonEvent.this);

        }
    },

    onLevelButtonEvent:function (sender,type) {//??
        // var level = sender.getTag();     // 当前等级
        // // TODO 加载关卡数据，进入游戏。
        // // 停止音乐
        // cc.audioEngine.stopMusic();
        // // 关卡设置
        // GameManager.setLevel(sender.getTag());
        // // 加载资源，并进入游戏
        // cc.LoaderScene.preload(g_gamePlay_resources, function () {
        //     GameManager.loadLevelData(GameManager.getLevel());
        //     cc.director.runScene(new GamePlayScene());
        // }, this);

        switch(type){
            case cc.Widget.TOUCH_ENDED:
                var level = sender.getTag();
                cc.audioEngine.stopMusic();
                GameManager.setLevel(level);
                break;
        }

    },

    //加载关卡
    loadlevel:function (level) {
        this.loadRoute(level);
        this.loadLevelEffects(level);
    },

    //加载【关卡道路】
    loadRoute:function (level) {
        cc.log("===loadRoute======");
        var node = null;
        for(var i =0; i< level - 1;i++){
            node = new cc.Sprite("#route_"+(i+1)+".png");
            if(i % 10 == 9){//?
                node.setAnchorPoint(cc.p(0,0.5));
            }
            node.x = node.width/2 + Math.floor(i%10)*node.width;
            node.y = this.scrollView.getInnerContainerSize().height/2;
            this.scrollView.addChild(node,this.zOrderMap.route);
        }
    },

    //加载按钮特效
    loadLevelEffects:function (level) {
        var index = level - 1;
        var  button = this.routeButtonArray[index];

        var node = new RouteButtonEffect();
        this.scrollView.addChild(node,this.zOrderMap.routeButtonEffect);
        node.setPosition(button.getPosition());
    },
});
